import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:myapp/api/Http.dart';
import 'package:myapp/api/Urls.dart';
import 'package:myapp/model/SearchResultModel.dart';
import 'package:myapp/page/home/SearchResultItem.dart';
import 'package:myapp/util/IGenericDataProvider.dart';
import 'package:myapp/widget/SearchBarDelegate.dart';
import 'package:myapp/widget/SearchTextField.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class SearchResultPage extends StatefulWidget {
  @override
  _SearchResultPageState createState() => _SearchResultPageState();
}

class _SearchResultPageState extends State<SearchResultPage> {
  RefreshController _refreshController =
      RefreshController(initialRefresh: true);
  int page = 1;
  String lastId = "";
  List<Goods_listBean> listBean = List();

  void _onRefresh() async {
    // monitor network fetch
    page = 1;
    lastId = "";
    getSearchData();
    // if failed,use refreshFailed()
  }

  void _onLoading() async {
    // monitor network fetch
    // if failed,use loadFailed(),if no data return,use LoadNodata()
    page++;
    getSearchData();
  }

  String queryStr = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    queryStr = IGenericDataProvider.of<String>(context);
    return Scaffold(
        appBar: AppBar(
          title: Text(
            queryStr,
            style: TextStyle(color: Colors.white),
          ),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                showSearch(context: context, delegate: SearchBarDelegate());
              },
            )
          ],
        ),
        body: IndexedStack(
          children: <Widget>[
//            SearchTextField(
//              hintTextStr: queryStr,
//            ),
            Container(
              child: SmartRefresher(
                enablePullDown: true,
                enablePullUp: true,
                header: MaterialClassicHeader(),
                footer: CustomFooter(
                  builder: (BuildContext context, LoadStatus mode) {
                    Widget body;
                    if (mode == LoadStatus.idle) {
                      body = Text("上拉加载");
                    } else if (mode == LoadStatus.loading) {
                      body = CupertinoActivityIndicator();
                    } else if (mode == LoadStatus.failed) {
                      body = Text("加载失败！点击重试！");
                    } else if (mode == LoadStatus.canLoading) {
                      body = Text("松手,加载更多!");
                    } else {
                      body = Text("没有更多数据了!");
                    }
                    return Container(
                      height: 55.0,
                      child: Center(child: body),
                    );
                  },
                ),
                controller: _refreshController,
                onRefresh: _onRefresh,
                onLoading: _onLoading,
                child: ListView.builder(
                  itemBuilder: (c, i) => Card(
                    child: Center(child: SearchResultItem(listBean[i])),
                    margin: const EdgeInsets.all(6),
                  ),
                  itemExtent: 100.0,
                  itemCount: listBean.length,
                ),
              ),
            )
          ],
        ));
  }

  void getSearchData() {
    DioUtils.postHttp(Urls.SearchGoodsUrl, parameters: {
      "keyword": queryStr,
      "page": page,
      "page_size": 10,
      "list_id": lastId
    }, onSuccess: (data) {
      print(data);
      SearchResultModel searchResultModel =
          SearchResultModel.fromMap(jsonDecode(data));
      lastId = searchResultModel.goodsSearchResponse.listId;
      if (page == 1) {
        _refreshController.refreshCompleted();
      } else {
        _refreshController.loadComplete();
      }
      setState(() {
        listBean.addAll(searchResultModel.goodsSearchResponse.goodsList);
      });
    }, onError: (err) {
      print(err);
    });
  }
}
